package com.keyancompany.zhongbaopingtai.activity;

import android.support.v7.widget.Toolbar;
import android.text.InputFilter;
import android.text.InputType;
import android.text.Spanned;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.keyancompany.zhongbaopingtai.R;
import com.keyancompany.zhongbaopingtai.api.UserAPI;
import com.keyancompany.zhongbaopingtai.model.BaseModel;
import com.keyancompany.zhongbaopingtai.utils.T;

import butterknife.Bind;
import butterknife.BindString;
import butterknife.OnClick;

/**
 * Created by firefox on 2017/3/28.
 */

public class BalanceTransferActivity extends BaseActivity {
    @BindString(R.string.balance_transfer_text)
    String title;

    @Bind(R.id.toolbar)
    Toolbar toolbar;
    @Bind(R.id.transfer_account)
    TextView transferAccount;
    @Bind(R.id.amount)
    EditText amount;
    @Bind(R.id.amount_hint)
    TextView amountHint;
    @Bind(R.id.pay_password)
    EditText payPassword;
    @Bind(R.id.sure_transfer)
    TextView sureTransfer;

    @Override
    protected int getContentViewId() {
        return R.layout.activity_balance_transfer;
    }

    @Override
    protected int getMenuId() {
        return 0;
    }

    @Override
    protected void initToolBar() {
        initToolBar(toolbar, title);
    }

    @Override
    protected void setUpViews() {
        transferAccount.setText(UserAPI.getCurrentUser().getZhifubao());

        amount.setInputType(InputType.TYPE_NUMBER_FLAG_DECIMAL | InputType.TYPE_CLASS_NUMBER);

        //设置字符过滤

        amount.setFilters(new InputFilter[]{new InputFilter() {
            @Override
            public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
                if (source.equals(".") && dest.toString().length() == 0) {
                    return "0.";
                }
                if (dest.toString().contains(".")) {
                    int index = dest.toString().indexOf(".");
                    int mlength = dest.toString().substring(index).length();
                    if (mlength == 3) {
                        return "";
                    }
                }
                return null;
            }
        }});
    }

    @OnClick(R.id.sure_transfer)
    public void sureTransfer(View v) {
        if (TextUtils.isEmpty(amount.getText().toString())) {
            Toast.makeText(mContext, "请输入提现金额", Toast.LENGTH_SHORT).show();
            return;
        }
        if (TextUtils.isEmpty(payPassword.getText().toString())) {
            Toast.makeText(mContext, "请输入支付密码", Toast.LENGTH_SHORT).show();
            return;
        }

        UserAPI.cashRequest(payPassword.getText().toString(), amount.getText().toString(), this, getLoading(-1));
    }

    @Override
    public void success(int type, String json) {
        super.success(type, json);
        BaseModel base = validateJson(json);
        if (base != null && base.isSuccess()) {
            T.showLong(mContext, base.msg);
            finish();
        }
    }
}
